// components/toast/toast.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    opacity: 0,
    shaList: [],
    shaTimer: null,
    padS: 15
  },
  ready: function () {
    var systeminfo = wx.getSystemInfoSync();

    //px为单位
    var sW = systeminfo.screenWidth;

    var rate = 750 / sW;
    console.log(rate);

    //rpx为单位
    var rW = parseInt(sW * rate)

    //准备的时候开始赋值
    this.setData({
      autoWidth: rW - 100,
      marginleft: 40,
      autoHeight: 1
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //显示taost
    show: function (shaList) {
      if (this.shaTimer) {
        clearTimeout(this.shaTimer)
      }

      console.log("shaList", shaList)

      //初始化将高度变为最低
      this.setData({
        autoHeight: 1
      })

      //创建一个动画
      var animation = wx.createAnimation({
        duration: 500,
        timingFunction: 'ease',
      })

      this.animation = animation
      //让组件显示
      animation.opacity(0.9).step()
      //赋值显示内容
      this.setData({
        shaList: shaList,
        autoHeight: 1000,
        animationData: animation.export()
      })
      //2秒后消失
      this.shaTimer = setTimeout(function () {
        animation.opacity(0).step()
        this.setData({
          animationData: animation.export(),
          autoHeight: 1
        })
      }.bind(this), 20000)
    },
    hide: function () {
      if (this.animation) {
        setTimeout(function () {
          this.animation.opacity(0).step()
          this.setData({
            animationData: this.animation.export(),
            autoHeight: 1
          })
        }.bind(this), 100)
      }
      if (this.shaTimer) {
        clearTimeout(this.shaTimer)
      }
    },
    showContent: function (e) {
      var type = e.mark.type
      this.triggerEvent('toast', type)
    },
    changeToastContent: function (shaList) {
      if (this.shaTimer) {
        clearTimeout(this.shaTimer)
      }

      this.setData({
        shaList: shaList,
      })

       //2秒后消失
       this.shaTimer = setTimeout(function () {
        this.animation.opacity(0).step()
        this.setData({
          animationData: this.animation.export(),
          autoHeight: 1
        })
      }.bind(this), 20000)

    }
  }
})
